package com.zatech.cgnci.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.CgnciCodeTypeAddDTO;
import com.zatech.cgnci.project.dto.CgnciCodeTypeQueryDTO;
import com.zatech.cgnci.project.dto.CgnciCodeTypeUpdateDTO;
import com.zatech.cgnci.project.entity.CgnciCodeTypeDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.service.CgnciCodeTypeService;
import com.zatech.cgnci.project.vo.AllCgnciCodeTypeVO;
import com.zatech.cgnci.project.vo.CgnciCodeTypeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 基础信息表 前端控制器
 * </p>
 *
 * @author Generator
 * @since 2024-12-17
 */
@RestController
@RequestMapping("/api/code-type")
@RequiredArgsConstructor
@Api(produces = "application/json", tags = "字典类型api接口文档")
public class CgnciCodeTypeController {

    private final CgnciCodeTypeService codeTypeService;

    @PostMapping("/pageHigherCode")
    @ApiOperation(value = "一级分页列表查询")
    public IPage<CgnciCodeTypeVO> pageHigherCode(@RequestBody CgnciCodeTypeQueryDTO queryDTO) {
        UserEntity user = UserContext.getUser();
        if(VerifyUtils.isEmpty(user)){
            return null;
        }
        return codeTypeService.pageHigherCode(queryDTO);
    }

    @PostMapping("/pageCode")
    @ApiOperation(value = "子集分页列表查询")
    public IPage<CgnciCodeTypeVO> pageCode(@RequestBody CgnciCodeTypeQueryDTO queryDTO) {
        UserEntity user = UserContext.getUser();
        if(VerifyUtils.isEmpty(user)){
            return null;
        }
        return codeTypeService.pageCode(queryDTO);
    }

    @GetMapping("/queryAll")
    @ApiOperation(value = "查询枚举列表")
    public Map<String,List<AllCgnciCodeTypeVO>> queryAll() {
        UserEntity user = UserContext.getUser();
        if(VerifyUtils.isEmpty(user)){
            return null;
        }
        return codeTypeService.queryAll();
    }

    @GetMapping("/findById")
    @ApiOperation(value = "详情")
    public CgnciCodeTypeVO findCodeTypeById(@RequestParam(value = "codeId") Long codeId) {
        CgnciCodeTypeDO codeTypeDO = codeTypeService.getById(codeId);
        return ConvertUtils.convert(codeTypeDO, CgnciCodeTypeVO.class);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public void add(@RequestBody @Valid CgnciCodeTypeAddDTO codeTypeAddDTO) {
        codeTypeService.addCode(codeTypeAddDTO);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public void update(@RequestBody @Valid CgnciCodeTypeUpdateDTO codeTypeUpdateDTO) {
        codeTypeService.updateCode(codeTypeUpdateDTO);
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "删除")
    public void deleteById(@RequestParam(value = "codeId") Long codeId) {
        codeTypeService.removeById(codeId);
    }

    @GetMapping("/deleteByIds")
    @ApiOperation(value = "批量删除")
    public void deleteByIds(@RequestParam(value = "codeIds") String codeIds) {
        List<Long> codeIdList = Arrays.stream(codeIds.split(",")).map(Long::valueOf).collect(Collectors.toList());
        codeTypeService.removeByIds(codeIdList);
    }
    
    @GetMapping(value = "/downloadTemplate")
    @ApiOperation("模板下载")
    public void downloadTemplate(HttpServletResponse response) {
        codeTypeService.downloadTemplate(response);
    }
}

